package com.oocl.im.gui;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.im.clinet.Client;

public class LoginFrame extends JFrame {
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JButton loginBtn;
	private JButton cancelBtn;
	private JLabel userName;
	private JLabel password;

	public LoginFrame() throws HeadlessException {
		super();
		this.setSize(400, 260);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		init();
		addEvent();
		this.setVisible(true);
		this.setTitle("Welcome to login !");
	}

	private void init() {
		this.setLayout(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		loginBtn = new JButton("login");
		cancelBtn = new JButton("cancel");
		userName = new JLabel("UserName:");
		password = new JLabel("Password:");
		nameTf.setBounds(120, 20, 230, 35);
		psdPf.setBounds(120, 80, 230, 35);
		loginBtn.setBounds(120, 160, 90, 40);
		cancelBtn.setBounds(260, 160, 90, 40);
		userName.setBounds(30,20,90,35);
		password.setBounds(30,80,90,35);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(cancelBtn);
		this.add(userName);
		this.add(password);
	}

	private void addEvent() {
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String name = nameTf.getText();
				if(!name .equals("")){
				setTitle("Loading...");
				for(int i=0;i<1000000000;i++);
				ChatFrame cf = new ChatFrame(name);
				dispose();
				}else{
					new ErrorFrame("Username or password can't null !!!");
				}
			}
		});
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("cancel login...");
				System.exit(0);

			}
		});
	}

	public static void main(String[] args) {
		new LoginFrame();
	}
}
